﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace FinalCountdown.Controls
{
    public partial class DateTimePicker : UserControl
    {
        public static readonly DependencyProperty FullDateTimeProperty =
            DependencyProperty.Register("DateTimeValue", typeof(DateTime?), typeof(DateTimePicker),
                                        new PropertyMetadata(null, FullDateTimePropertyChanged));

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(DateTimePicker),
                                        new PropertyMetadata("Label", LabelPropertyChanged));

        public DateTimePicker()
        {
            InitializeComponent();
            ClearControls();
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public DateTime? DateTimeValue
        {
            get
            {
                if (DateCtr.Value.HasValue && TimeCtr.Value.HasValue)
                {
                    DateTime dt =
                        new DateTime(DateCtr.Value.Value.Year, DateCtr.Value.Value.Month, DateCtr.Value.Value.Day,
                                     TimeCtr.Value.Value.Hour, TimeCtr.Value.Value.Minute, 0).ToUniversalTime();

                    return DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                }
                else return null;
            }
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            ClearControls();
        }

        private void ClearControls()
        {
            DateCtr.Value = null;
            TimeCtr.Value = new DateTime(1900, 01, 01, 0, 0, 0);
            SetValue(FullDateTimeProperty, null);
        }

        private static void FullDateTimePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //Csak akkor állítjuk be a controlokat, ha korábban nem volt értékük
            var ctrl = d as DateTimePicker;
            if (ctrl == null) return;

            DatePicker dateCtrl = ctrl.DateCtr;
            TimePicker timeCtrl = ctrl.TimeCtr;

            if (dateCtrl == null || timeCtrl == null) return;

            var newDateTime = e.NewValue as DateTime?;

            if (newDateTime.HasValue)
            {
                if (dateCtrl.Value == null && timeCtrl.Value == new DateTime(1900, 1, 1, 0, 0, 0))
                {
                    dateCtrl.Value = new DateTime(newDateTime.Value.Year, newDateTime.Value.Month, newDateTime.Value.Day);
                    timeCtrl.Value = new DateTime(1900, 1, 1, newDateTime.Value.Hour, newDateTime.Value.Minute,
                                                  newDateTime.Value.Second);
                }
            }
        }

        private static void LabelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as DateTimePicker;

            if (ctrl != null)
            {
                ctrl.LabelDateCtr.Text = e.NewValue.ToString();
            }
        }

        private void SetFullDateTime()
        {
            if (DateCtr.Value.HasValue && TimeCtr.Value.HasValue)
            {
                var actualDateTime = new DateTime(DateCtr.Value.Value.Year, DateCtr.Value.Value.Month,
                                                  DateCtr.Value.Value.Day, TimeCtr.Value.Value.Hour,
                                                  TimeCtr.Value.Value.Minute, 0);
                //actualDateTime = actualDateTime.ToUniversalTime();
                DateTime newDateTime = DateTime.SpecifyKind(actualDateTime, DateTimeKind.Utc);
                SetValue(FullDateTimeProperty, newDateTime);
            }
        }

        private void DateCtr_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            SetFullDateTime();
        }

        private void TimeCtr_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            SetFullDateTime();
        }
    }
}